﻿// TestCaseGeneratorUI
// (c)2007 Mark Bulmahn. mBulmahn@web.de
// License: GPL 2.0
// Project: TestCaseGenerator. http://www.codeplex.com/TestCaseGenerator

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TestCaseModel;

namespace TestCaseGenerator.Controls
{
    public partial class ResultsEditorControl : UserControl
    {
        private const string NEW_RESULT = "Neues Ergebnis";

        ExpectedResults results = null;
        public ExpectedResults Results
        {
            get { return results; }
            set 
            { 
                results = value;
                if (results != null)
                    Resync();
            }
        }

        public ResultsEditorControl()
        {
            InitializeComponent();
        }

        private void Resync()
        {
            listResults.Items.Clear();
            foreach (ExpectedResult r in results)
            {
                ListViewItem newItem = CreateNewLVItem(r);
                listResults.Items.Add(newItem);
            }

            listResults.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        private static ListViewItem CreateNewLVItem(ExpectedResult r)
        {
            ListViewItem newItem = new ListViewItem(r.Id);
            newItem.SubItems.Add(new ListViewItem.ListViewSubItem(newItem, r.Desc));
            newItem.SubItems.Add(new ListViewItem.ListViewSubItem(newItem, r.ResTypeText));
            return newItem;
        }
        
        private bool RenameItem(string oldText, string newText)
        {
            bool res = true;
            if ((oldText.Trim() != "") && (newText.Trim() != ""))
            {
                results.ChangeId(oldText, newText);
            }
            else
                res = false;

            return res;
        }

        private void EditItem()
        {
            ExpectedResult itemToEdit = results.GetById(listResults.SelectedItems[0].Text);
            FrmEditResult resultEditor = new FrmEditResult(itemToEdit);
            if (resultEditor.ShowDialog() == DialogResult.OK)
            {
                Resync();
            }
        }

        private void NewItem()
        {
            if (results.GetById(NEW_RESULT) == null)
            {
                ExpectedResult newResult = new ExpectedResult(NEW_RESULT);
                results.Add(newResult);
                ListViewItem newLvItem = CreateNewLVItem(newResult);
                listResults.Items.Add(newLvItem);
                newLvItem.BeginEdit();
            }
            else
            {
                MessageBox.Show("Es existiert bereits ein Ergebnis\nmit der Bezeichnung '" +
                    NEW_RESULT + "'");
            }
        }

        private void listResults_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (e.Label != null)
            {
                string oldText = listResults.Items[e.Item].Text;
                string newText = e.Label;
                if (!RenameItem(oldText, newText))
                {
                    e.CancelEdit = true;
                }
            }
        }

        private void butDel_Click(object sender, EventArgs e)
        {
            if (listResults.SelectedItems.Count > 0)
            {
                string id = listResults.SelectedItems[0].Text;
                ExpectedResult itemToDelete = results.GetById(id);
                results.Remove(itemToDelete);
                listResults.SelectedItems[0].Remove();
            }
        }

        private void listResults_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listResults_DoubleClick(object sender, EventArgs e)
        {
            if (listResults.SelectedItems.Count > 0)
            {
                EditItem();
            }
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            NewItem();
        }
    }
}
